package messagesvc.executor.subcribe;


import cn.hutool.core.collection.CollectionUtil;
import cn.ucloud.microsf.commonlib.enums.ResponseCode;
import cn.ucloud.microsf.commonlib.response.EmptyResponse;
import cn.ucloud.microsf.messagesvc.biz.SubscribeService;
import cn.ucloud.microsf.messagesvc.orm.entity.SubscribeEntity;
import cn.ucloud.microsf.messagesvc.request.record.SubscribeCreateRequest;
import cn.ucloud.microsf.mpcsvc.common.enums.DataStatus;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 消息订阅
 */
@Slf4j
@Service
public class SubscribeCreateExecutor {

  @Autowired
  private SubscribeService subscribeService;

  public EmptyResponse execute(SubscribeCreateRequest request) {

    if (CollectionUtil.isEmpty(request.getTemplate())){
      return EmptyResponse.builder()
          .code(ResponseCode.CODE_PARAMETER_ERROR)
          .msg("模板不能为空！")
          .build();
    }

    List<SubscribeEntity> entities = new ArrayList<>();
    for (String tempUuid : request.getTemplate()) {
      SubscribeEntity entity = new SubscribeEntity();
      entity.setSubId(request.getSubId());
      entity.setSubName(request.getSubName());
      entity.setTmpUuid(tempUuid);
      entity.setStatus(DataStatus.VALID);
      entities.add(entity);
    }
    subscribeService.saveBatch(entities);

    return EmptyResponse.builder()
        .code(ResponseCode.CODE_SUCCESS)
        .msg(ResponseCode.CODE_SUCCESS.getDefaultMessage())
        .build();

  }

}

